package com.hera.base.web.vaadin.mantenimiento;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseListadoListener;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.rest.delegate.SecurityDelegate;
import com.hera.process.security.domain.User;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class ListadoPGUSerUI extends BaseUI implements
		MantenimientoMaestroDetalle, ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private TableCustom tblfProcessGroupUser;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BaseListadoListener ventanapop;

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	public ListadoPGUSerUI(BaseListadoListener ventanapop) {
		this.ventanapop = ventanapop;
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
	}

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();

	}

	private static final Logger log = Logger.getLogger(ListadoPGUSerUI.class);

	@Autowired
	SecurityDelegate securityDelegate;
	boolean esNuevo;
	boolean esNuevoDetalle;

	User user;

	@Override
	public void buttonClick(ClickEvent event) {
		ventanapop.recibirDatos(user);
	}

	@Override
	public void cargarFormulario() {
		log.info("inicio cargar formulario");
		esNuevo = true;

		user = null;

		tblfProcessGroupUser.setTitulo(" LISTADO DE USUARIOS ");
		tblfProcessGroupUser.setHeight("210px");

		tblfProcessGroupUser.addColumn("userLogin", "Login", 150, true,
				String.class);
		tblfProcessGroupUser.addColumn("name", "Nombre", 150, true,
				String.class);
		tblfProcessGroupUser.addColumn("lastname", "Materno", 100, true,
				String.class);
		tblfProcessGroupUser.addColumn("motherlast", "Paterno", 100, true,
				String.class);

		tblfProcessGroupUser.construir();
		tblfProcessGroupUser.addTextChangeListener(new TextChangeListener() {

			/**
		 * 
		 */
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);

			}
		});
		tblfProcessGroupUser.addValueChangeListener(new ValueChangeListener() {

			/**
		 * 
		 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				user = (User) tblfProcessGroupUser.getSelectedItem(event);
				esNuevo = (user == null);
				ventanapop.recibirDatos(user);
				((Window) getParent().getParent()).close();
			}
		});

		log.info("FIN LISTADO");

	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);

	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio buscarMaestro");
		FiltroDTO filtroDTO = tblfProcessGroupUser.getFiltroDTO(event);
		List<User> lista = securityDelegate.findAllSysUsers(filtroDTO);
		tblfProcessGroupUser.setLstData(lista);
		limpiarMaestro();
		log.info("fin buscarMaestro");

	}

	@Override
	public void limpiarMaestro() {
		// TODO Auto-generated method stub

	}

	@Override
	public void eliminarMaestro() {
		// TODO Auto-generated method stub

	}

	@Override
	public void guardarMaestro() {
		// TODO Auto-generated method stub

	}

	@Override
	public void buscarDetalle() {
		// TODO Auto-generated method stub

	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void limpiarDetalle() {
		// TODO Auto-generated method stub

	}

	@Override
	public void eliminarDetalle() {
		// TODO Auto-generated method stub

	}

	@Override
	public void guardarDetalle() {
		// TODO Auto-generated method stub

	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);

		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");

		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);

		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("100.0%");
		verticalLayout_2.setHeight("-1px");
		verticalLayout_2.setMargin(true);
		verticalLayout_2.setSpacing(true);

		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		verticalLayout_2.addComponent(horizontalLayout_1);

		return verticalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("100.0%");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		horizontalLayout_1.setSpacing(true);

		// pnlFormulario
		pnlFormulario = buildPnlFormulario();
		horizontalLayout_1.addComponent(pnlFormulario);

		return horizontalLayout_1;
	}

	@AutoGenerated
	private VerticalLayout buildPnlFormulario() {
		// common part: create layout
		pnlFormulario = new VerticalLayout();
		pnlFormulario.setImmediate(false);
		pnlFormulario.setWidth("100.0%");
		pnlFormulario.setHeight("-1px");
		pnlFormulario.setMargin(false);

		// tblfProcessGroupUser
		tblfProcessGroupUser = new TableCustom();
		tblfProcessGroupUser.setImmediate(false);
		tblfProcessGroupUser.setWidth("-1px");
		tblfProcessGroupUser.setHeight("-1px");
		pnlFormulario.addComponent(tblfProcessGroupUser);

		return pnlFormulario;
	}

}
